package flc.ast.fragment;

import android.app.Dialog;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.cartoonavatarmaker.lib.core.AvatarRole;
import com.stark.cartoonavatarmaker.lib.core.AvatarRoleDbUtil;
import com.stark.cartoonavatarmaker.lib.core.AvatarUtil;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.DressUpActivity;
import flc.ast.activity.PreviewActivity;
import flc.ast.adapter.RoleAdapter;
import flc.ast.adapter.FuncAdapter;
import flc.ast.bean.MyDrawRecordBean;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.MyAudioPlayer;
import flc.ast.utils.SoundManager;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private RoleAdapter roleAdapter;
    private FuncAdapter funcAdapter;
    private List<AvatarRole> listShow = new ArrayList<>();
    private int oldPosition = 1;
    private Dialog myDeleteDialog;
    private boolean hasAudio;


    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivBackgroundMusic.setOnClickListener(this);
        mDataBinding.ivCharacterCamera.setOnClickListener(this);
        mDataBinding.ivCharacterDelete.setOnClickListener(this);

        MyAudioPlayer.getInstance().play();
        hasAudio = SoundManager.getInstance().isCanPlay();

        //角色
        mDataBinding.rvRoleList.setLayoutManager(new GridLayoutManager(mContext, 3));
        roleAdapter = new RoleAdapter();
        mDataBinding.rvRoleList.setAdapter(roleAdapter);
        roleAdapter.setOnItemClickListener(this);
        //收藏
        mDataBinding.rvFuncList.setLayoutManager(new GridLayoutManager(mContext, 3));
        funcAdapter = new FuncAdapter();
        mDataBinding.rvFuncList.setAdapter(funcAdapter);
        funcAdapter.setOnItemClickListener(this);

        //删除弹框
        deleteDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivBackgroundMusic://背景音乐
                hasAudio = !hasAudio;
                SoundManager.getInstance().setCanPlay(hasAudio);
                if (hasAudio) {
                    MyAudioPlayer.getInstance().play();
                    mDataBinding.ivBackgroundMusic.setImageResource(R.drawable.icon_yy);
                } else {
                    MyAudioPlayer.getInstance().stop();
                    mDataBinding.ivBackgroundMusic.setImageResource(R.drawable.icon_yya);
                }
                break;
            case R.id.ivCharacterCamera://拍照
                if (listShow.size() < 2) {
                    ToastUtils.showShort(getString(R.string.please_create_role));
                    return;
                }
                PreviewActivity.imgPath = roleAdapter.getItem(oldPosition).imgUri;
                startActivity(PreviewActivity.class);
                break;
            case R.id.ivCharacterDelete://删除角色
                if (listShow.size() < 2) {
                    ToastUtils.showShort(getString(R.string.please_create_role));
                    return;
                }
                myDeleteDialog.show();
                break;
            //删除弹框
            case R.id.ivDialogDeleteCancel://取消
                myDeleteDialog.dismiss();
                break;
            case R.id.ivDialogDeleteRight://确定
                myDeleteDialog.dismiss();
                delete();
                break;

        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == roleAdapter) {
            if (position == 0) {
                DressUpActivity.start(mContext, null, 0);
            } else {
                roleAdapter.getItem(oldPosition).setSelected(false);
                roleAdapter.notifyItemChanged(oldPosition);
                oldPosition = position;
                roleAdapter.getItem(position).setSelected(true);
                roleAdapter.notifyItemChanged(position);
                Glide.with(mContext).load(roleAdapter.getItem(position).imgUri).into(mDataBinding.ivCharacterImg);
            }
        } else if (adapter == funcAdapter) {
            if (listShow.size() < 2) {
                ToastUtils.showShort(getString(R.string.please_create_role));
                return;
            }
            switch (position) {
                case 0:
                    DressUpActivity.start(mContext, roleAdapter.getItem(oldPosition), 5);
                    break;
                case 1:
                    DressUpActivity.start(mContext, roleAdapter.getItem(oldPosition), 2);
                    break;
                case 2:
                    DressUpActivity.start(mContext, roleAdapter.getItem(oldPosition), 6);
                    break;
                case 3:
                    DressUpActivity.start(mContext, roleAdapter.getItem(oldPosition), 7);
                    break;
                case 4:
                    DressUpActivity.start(mContext, roleAdapter.getItem(oldPosition), 14);
                    break;
                case 5:
                    DressUpActivity.start(mContext, roleAdapter.getItem(oldPosition), 4);
                    break;
                case 6:
                    DressUpActivity.start(mContext, roleAdapter.getItem(oldPosition), 13);
                    break;
                case 7:
                    DressUpActivity.start(mContext, roleAdapter.getItem(oldPosition), 3);
                    break;
                case 8:
                    DressUpActivity.start(mContext, roleAdapter.getItem(oldPosition), 16);
                    break;
            }
        }
    }

    @Override
    protected void initData() {
        //自定义角色
        List<Integer> listFunc = new ArrayList<>();
        listFunc.add(R.drawable.zdy_lx);
        listFunc.add(R.drawable.zdy_fs);
        listFunc.add(R.drawable.zdy_sh);
        listFunc.add(R.drawable.zdy_wg);
        listFunc.add(R.drawable.zdy_fx);
        listFunc.add(R.drawable.zdy_ts);
        listFunc.add(R.drawable.zdy_yj);
        listFunc.add(R.drawable.zdy_fz);
        listFunc.add(R.drawable.zdy_da);
        funcAdapter.setList(listFunc);
    }

    @Override
    public void onResume() {
        super.onResume();
        //角色
        getData();
    }

    private void getData() {
        oldPosition = 1;
        listShow.clear();
        listShow = AvatarRoleDbUtil.getAvatarRoles();
        listShow.add(0, new AvatarRole());
        if (listShow.size() > 1) {
            listShow.get(1).setSelected(true);
            Glide.with(mContext).load(listShow.get(1).imgUri).into(mDataBinding.ivCharacterImg);
        } else {
            mDataBinding.ivCharacterImg.setImageResource(0);
        }
        roleAdapter.setList(listShow);
    }

    //删除
    private void delete() {
        showDialog(getString(R.string.delete_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        AvatarRoleDbUtil.delete(roleAdapter.getItem(oldPosition));
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        getData();
                        dismissDialog();
                    }
                });
            }
        }, 500);
    }

    //删除弹框
    private void deleteDialog() {
        myDeleteDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_delete, null);
        myDeleteDialog.setContentView(inflate);
        myDeleteDialog.setCancelable(false);
        Window dialogWindow = myDeleteDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView dialogCancel = inflate.findViewById(R.id.ivDialogDeleteCancel);
        ImageView dialogRight = inflate.findViewById(R.id.ivDialogDeleteRight);
        dialogCancel.setOnClickListener(this);
        dialogRight.setOnClickListener(this);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            MyAudioPlayer.getInstance().stop();
        } else {
            if (hasAudio) {
                MyAudioPlayer.getInstance().play();
                mDataBinding.ivBackgroundMusic.setImageResource(R.drawable.icon_yy);
            } else {
                MyAudioPlayer.getInstance().stop();
                mDataBinding.ivBackgroundMusic.setImageResource(R.drawable.icon_yya);
            }
        }
    }
}